package db

var User = newUserModel()

type userModel struct {
	ID        int    `xorm:"'id' INT NOT NULL PRIMARY KEY AUTO_INCREMENT comment('主键')"`
	Uname     string `xorm:"'username' VARCHAR(32) NOT NULL comment('用户名')"`
	Passwd    string `xorm:"'password' VARCHAR(128) NOT NULL comment('密码')"`
	CreatedAt int64  `xorm:"'id' INT NOT NULL default 0 comment('创建时间')"`
}

func newUserModel() *userModel {
	return &userModel{}
}

func (c *userModel) TableName() string {
	return "users"
}

func (this *userModel) Login(username, password string) (*userModel, error) {

	var user userModel
	has, err := DB.Table(this).Where("username=?", username).Where("password=?", password).Get(&user)

	if !has {
		return nil, err
	}

	return &user, err
}
